package fourfaith.com.ff_sl21_app.utils;

import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.nineoldandroids.view.ViewPropertyAnimator;

import java.util.Map;

/**
 * listview item 展开简单动画
 * Created by Admin on 2017/4/24.
 */

public class ItemAnimation {

    /**
     * 展开
     * @param v
     * @param height
     */
    public static void show(final View v , int height){
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = ValueAnimator.ofInt(0, height);
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                v.getLayoutParams().height = value;
                v.setLayoutParams(v.getLayoutParams());
            }
        });
        animator.start();
    }

    /**
     * 收起
     * @param v
     * @param height
     */
    public static void hide(final View v,int height){

        ValueAnimator animator = ValueAnimator.ofInt(height, 0);
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                if(value==0){
                    v.setVisibility(View.GONE);
                }
                v.getLayoutParams().height = value;
                v.setLayoutParams(v.getLayoutParams());
            }
        });
        animator.start();
    }

    /**
     * 旋转切换图标
     * @param v
     * @param item
     */
    public static void rotateSwitcherIcon(View v, Map<String, Object> item, float angle)
    {
        float degree = (boolean)item.get("isOpen") ? angle : 0;
        ViewPropertyAnimator.animate(v)
                .setDuration(500)
                .setInterpolator(new AccelerateDecelerateInterpolator())
                .rotation(degree)
                .start();
    }
}
